;(function(){
	'use strict';
	angular.module('services.userBehaviorAnalysis.srPerceptionUwrAnalysis',[])
	.factory('srPerceptionUwrAlSevice',srPerceptionUwrAlSevice);
	function srPerceptionUwrAlSevice(){
		return {
      getEchartsData: getEchartsData,
      getEchartsPieData: getEchartsPieData,
			getEchartsLineData:getEchartsLineData,
			getOption:getOption,
			getArrayData: getArrayData,
			exportView: exportView
		};
  //柱状图
  function getEchartsData(xAxisData,seriesData,title,legendName){
    return {
      title: {
        text: title,
        x: 'center'
      },
      animation: false,
      tooltip: {
          trigger: 'item'
      },
      grid: {
        y: 100
      },
      legend: {
        data: [legendName],
        orient: 'vertical',
        x: 'right',
        y: 70
      },
      xAxis: [{
        type: 'category',
        data: xAxisData
      }],
      yAxis: [
        {
           type : 'value'
        }
      ],
      series: [
        {
          name: legendName,
          type: 'bar',
          data: seriesData
        }
      ]
    };
  }
  //饼图
  function getEchartsPieData(items,dimension,title){
    var seriesData = [];
    if(dimension === 'p'){
      angular.forEach(items,function(item){
        seriesData.push({value:item.churnRate.substring(0,item.churnRate.length-1),name:item.provinceCodeName});
      });
    }else{
      angular.forEach(items,function(item){
        seriesData.push({value:item.churnRate.substring(0,item.churnRate.length-1),name:item.businessLineName});
      });
    }
    var option = {
      title: {
        text: title,
        x: 'center'
      },
      tooltip : {
        trigger: 'item',
        formatter: '{b} :{c}%'
      },
      animation:false,
      series : [{
        type:'pie',
        radius : '60%',
        center: ['50%', '50%'],
        data:seriesData
      }]
    };
    return option;
  }
	//拆线图
  function getEchartsLineData(items,resource,title){
    var legendName = ['天猫旗舰店','移动商城','银行总对总'];
    var temp = {
      xData:items.xData,
      legendName:{},
      seriesData: [],
    };
    angular.forEach(legendName,function(ln){
    	temp.legendName[ln] = [];
    });
    angular.forEach(resource,function(res){
      temp.legendName['天猫旗舰店'].push(Number(res.tmallRate.substring(0,res.tmallRate.length-1)));
      temp.legendName['移动商城'].push(Number(res.mallRate.substring(0,res.mallRate.length-1)));
      temp.legendName['银行总对总'].push(Number(res.bankRate.substring(0,res.bankRate.length-1)));
    });
    angular.forEach(legendName,function(ln,index){
      temp.seriesData[index] = {
        name: ln,
        type: 'line',
        data: temp.legendName[ln]
      };
    });
    return getOption(temp,title,legendName);
  }
  //折线图option实例
  function getOption(temp,echartstitle,legendName,title){
    return {
      title: {
        text: echartstitle,
        x: 'center'
      },
      animation: false,
      tooltip : {
        
        formatter: '{a} : <br/>{b} : {c}%'
      },
      grid: {
        x2: 130
      },
      legend: {
        data: legendName,
        orient: 'vertical',
        x: 'right',
        y: 'center'
      },
      xAxis: [{
        type: 'category',
        data: temp.xData
      }],
      yAxis: [{
        type : 'value'
      }],
      series: temp.seriesData 
    };
  }
  //获取页面表头信息和echarts图x轴数据
  function getArrayData(data){
    var array = {
      xData:[],
      tableTh:[]
    };
    angular.forEach(data,function(d){
      if(array.xData.indexOf(d.time) === -1){
        array.xData.push(d.time);
      }
      angular.forEach(d.lineData,function(item){
        if(array.tableTh.indexOf(item.filedName) === -1){
          array.tableTh.push(item.filedName);
        }
      });
    });
    return array;
  }
  //导出
  function exportView(myEcharts,send) {
    send.base64s = [];
    var outData = '';
    var b = myEcharts.getDataURL('png');
    send.base64s.push(b.split(',')[1]);
    outData = JSON.stringify(send);
    return outData;
  }
}
})();